/******************************************************************
 * Created by: GOH CHEE HONG                                      
 * Date of Creation: 8/AUG/2008     
 * Date of last modification:
 *                                                                
 * Description: ADD A FRIEND
 ******************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Bcpg;
using System.IO;
using System.Collections;
namespace Pbdms.Forms.Tools
{
    public partial class FrmAddFriend : Form
    {
        private string keyId;
        private PgpPublicKey memKey;
        public FrmAddFriend()
        {
            
            InitializeComponent();
            Pbdms.Common.Global.paintBackGround(this);
        }
        /// <summary>
        /// browse for the public key
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFileBrowser_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDia.ShowDialog();
            string filelocation = null;
            if (result == DialogResult.OK)
            {
                Boolean isPublicKey = false;
                filelocation = openFileDia.FileName;
                Stream fileIn = File.OpenRead(filelocation);
                Stream inputStream = null;
                try
                {
                    inputStream = PgpUtilities.GetDecoderStream(fileIn);
                    PgpPublicKeyRing t = new PgpPublicKeyRing(inputStream);
                    keyId = t.GetPublicKey().KeyId.ToString("X");
                    memKey = t.GetPublicKey();
                    isPublicKey = true;
                }
                catch (Exception ex)
                {
                    isPublicKey = false;
                    Common.GUI.MsgBoxOneButton msgForm = new Pbdms.Common.GUI.MsgBoxOneButton("This is not a valid public key", 'E');
                    msgForm.Show();
                }
                finally
                {
                    inputStream.Close();
                    fileIn.Close();
                }
                if (isPublicKey == true)
                {
                    txtPubKey.Text = filelocation;
                }
                else
                {
                    txtPubKey.Text = "";
                }
            }
            else
            {


            }

        }
        /// <summary>
        /// cancel event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }
        /// <summary>
        /// Add a friend method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (check() == true)
            {
                if (checkNameIsInUse() == false)
                {

                    File.Copy(txtPubKey.Text, Common.Global.startupPath()+"\\Friends' public key\\" + keyId + ".asc",true);
                    string userInfo="";
                    foreach(string userinfo in memKey.GetUserIds())
                    {
                        userInfo = userinfo;
                        break;
                    }      
                  
  
                    Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().appendBuddyToDB(txtName.Text, keyId);
                    Program.friendControl.addFriend(buddy);
                    Program.mainForm.frmKeyManager.addKey(buddy.Name, buddy.KeyID, Common.Global.convertFingerPrint(memKey.GetFingerprint()), userInfo, false);
                    Program.mainForm.frmBuddiesStatus.peerTrackingManager.addATracker(buddy.KeyID, false);
                    Program.mainForm.frmKeyManager.Show();
                 
                   
                    this.DialogResult = DialogResult.OK;
                    
                    this.Close();
                }
                else
                {
                    Common.GUI.MsgBoxOneButton msgForm = new Pbdms.Common.GUI.MsgBoxOneButton("Please change your buddy name as it is used by one of your buddies", 'E');
                    msgForm.Show();
                }
            }
            else
            {
                Common.GUI.MsgBoxOneButton msgForm = new Pbdms.Common.GUI.MsgBoxOneButton("Some of the fields are missing", 'E');
                msgForm.Show();
            }
        }
        /// <summary>
        /// check for errors
        /// </summary>
        /// <returns></returns>
        private Boolean check()
        {
            if (txtName.Text.Trim().Equals("") ||txtPubKey.Text.Trim().Equals("") )
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// check whether the name is in use
        /// </summary>
        /// <returns></returns>
        private Boolean checkNameIsInUse()
        {

            ArrayList buddylist= BusinessLogic.DataController.BuddyController.create().getListOfBuddies();
            foreach (Common.DataObject.Buddy bud in buddylist)
            {
                if (bud.Name.Trim().Equals(txtName.Text.Trim()))
                {
                    return true;
                }
            }
            return false;
        }
    }
}